package com.nccsjz.back.merch.service;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import com.nccsjz.base.BaseService;
import com.nccsjz.pojo.Merchant;
import com.nccsjz.pojo.News;
import com.nccsjz.back.merch.dao.MerchDAO;
import com.nccsjz.back.news.dao.NewsDAO;
import com.nccsjz.utils.Pager;
/**
 * 用于处理商户管理相关的业务逻辑
 * ＠author stone
 * @version 1.0
 * */
public class MerchService  extends BaseService{
	//根据主键查看一条商户
	public News getNews(long id) throws SQLException{
		News news=null;
		try{
			openCon();
			news=new NewsDAO(con).getNews(id);
		}finally{
			closeCon();
		}
		return news;
	}
	//添加一条商户
	public boolean addMerch(Merchant merch) throws SQLException{
		boolean flag=false;
		try{
			openCon();
			flag=new MerchDAO(con).addMerch(merch);
		}finally{
			closeCon();
		}
		return flag;
	}
	//删除一条商户
	public boolean deleteMerch(long id) throws SQLException{
		boolean flag=false;
		try{
			openCon();
			flag=new MerchDAO(con).deleteMerch(id);
			
		}finally{
			closeCon();
		}
		return flag;
	}
	//分有处理商户
	public Pager getMerch(int pageSize,int pageNo) throws SQLException{
		Pager pager=null;
		try{
			openCon();
			pager=new MerchDAO(con).getMerchs(pageSize, pageNo);
		}finally{
			closeCon();
		}
		return pager;
	}
	
	public Pager getMoreMerch(int pageSize,int pageNo) throws SQLException{
		Pager pager=null;
		try{
			openCon();
			pager=new MerchDAO(con).getMoreMerchs(pageSize, pageNo);
		}finally{
			closeCon();
		}
		return pager;
	}
	
	
	//修改商户
	public boolean updateMerch(Merchant merch) throws SQLException{
		boolean flag=false;
		try{
			openCon();
			flag=new MerchDAO(con).updateMerch(merch);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		finally{
			closeCon();
		}
		return flag;
	}
	//批量删除商户
	public boolean deleteBatchNews(List<Long> list) throws SQLException{
		boolean flag=false;
		try{
			openCon();
			con.setAutoCommit(false);
			flag=new NewsDAO(con).deleteBathNews(list);
			con.commit();
		}finally{
			closeCon();
		}
		return flag;
	}
	
	//add by changpf
	 /**
	  * 得到前n条商户。
	  * @return
	  */
	public List<Merchant> getTopN(int n) {
		List<Merchant> merchs=null;
		try {
			openCon();
			merchs=new MerchDAO(con).getTopN(n);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				closeCon();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	return merchs;
	}
	/**
	 * 根据ID得到商户信息。
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public Merchant getMerchById(long id) throws Exception{
		this.openCon();
		Merchant merch=new MerchDAO(con).getMerch(id); 
		this.closeCon();
		return merch;
	}
	
	/**
	 * 根据用户名和密码取得商户
	 * @param name
	 * @param pwd
	 * @return
	 * @throws SQLException
	 * @throws IOException 
	 */
	public Merchant getMerchByNameAndPwd(String name,String pwd) throws SQLException, IOException{
		Merchant merch=null;
		this.openCon();
		merch=new MerchDAO(con).getMerchByNameAndPwd(name, pwd);
		this.closeCon();
		return merch;
	}
	/**
	 * 商户自己修改
	 * @param merch
	 * @return
	 * @throws SQLException
	 */
	public boolean updateMerch1(Merchant merch) throws SQLException {
		boolean flag=false;
		try{
			openCon();
			flag=new MerchDAO(con).updateMerch1(merch);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		finally{
			closeCon();
		}
		return flag;
	}
	
	/**
	 * 按照商户名称查询
	 * @param pageSize
	 * @param pageNo
	 * @param merch
	 * @return
	 * @throws SQLException
	 */
	public Pager getMerch(int pageSize, int pageNo, Merchant merch) throws SQLException {
		Pager pager=null;
		try{
			openCon();
			pager=new MerchDAO(con).getMerchs(pageSize, pageNo,merch);
		}finally{
			closeCon();
		}
		return pager;
	}
	
	/**
	 * 检查注册的商户名是否已经存在
	 * 
	 * @param username
	 * @param type
	 * @return
	 * @throws SQLException 
	 */
	public boolean isExists(String username) throws SQLException{
		boolean flag = false;
		try{
			openCon();
			flag = new MerchDAO(con).isMerchExistsByName(username);
		}finally{
			closeCon();
		}
		return flag;
	}
	
	/**
	 * initPassword方法根据商户编号初始化商户的密码
	 * 
	 * @param id
	 * @throws SQLException
	 */
	public void initPassword(Long id) throws SQLException{
		try{
			openCon();
			new MerchDAO(con).initMerchPasswordById(id);
		}finally{
			closeCon();
		}
	}
	
}
